<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class RelatedQuestionsWidget extends Widget
{

    /**
     *
     * @param $data = array('qid' => 1, 'title' => 'qqas');
     */
    public function render($data)
    {
        $Question = D('Question');

        $relatedQuestions = array();
        $tagEnable = get_site_config('tag_enable');
        $relatedQuestionSize = get_site_config('related_question_size');

        if ($tagEnable && $relatedQuestionSize > 0)
        {
            $tagIDs = $Question->getQuestionTagIDs($data['qid']);
        	if (!empty($tagIDs))
        	{
        		$RelatedView = D('QuestionTagUserView');
    			$where = array(
    			    'Question.question_id' => array('neq', $data['qid']),
    			    'tag_id' => array('in', $tagIDs)
    			);
    			$limit = $relatedQuestionSize;
    			$group = 'question_id';
    			$relatedQuestions = $RelatedView->where($where)->group($group)->limit($limit)->select();
        	}
        }

        if (empty($relatedQuestions) && $relatedQuestionSize > 0)
        {
        	$relatedQuestions = $Question->getSimilarQuestions($data['title'], $relatedQuestionSize);
        }

        $data['relatedQuestions'] = $relatedQuestions;

        $tplFile = TEMPLATE_PATH .'/Home/Widget_relatedQuestions.html';
        $content = $this->renderFile($tplFile, $data);
        return $content;
    }

}

/* End of file RelatedQuestionWidget.class.php */
/* Location: ./App/Lib/Widget/RelatedQuestionWidget.class.php */
